import datetime
import time
from unittest import expectedFailure

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

# # 打开浏览器 -- 驱动程序
# ChromeIns=ChromeDriverManager().install()
# #### 创建谷歌浏览器驱动对象
# driver=webdriver.Chrome(service=Service(ChromeIns))
# #输入网址（完整）： http://www.baidu.com
# driver.get("http://www.baidu.com")
# #找到百度输入框，输入关键字
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("高考查分")
# #点击百度一下的按钮
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# #关闭浏览器
# driver.quit()



#打开浏览器 -- 创建驱动
ChromeIns=ChromeDriverManager().install()
#浏览器参数配置
options = webdriver.ChromeOptions()
#添加无头模式
options.add_argument("-headless")
#####创建谷歌浏览器驱动对象
driver=webdriver.Chrome(service=Service(ChromeIns),options=options)

# driver.get("https://www.baidu.com/")
# ret=driver.find_elements(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li")
# for i in ret:
#     print(i.text) #获取文本的每个元素对应的文本信息


#点击事件
#查找元素+点击元素
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# ele=driver.find_element(By.CSS_SELECTOR,"#su")
# ele.click()

# driver.find_element(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(2) > a > span.title-content-title").click()

# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("美女")
# ele=driver.find_element(By.CSS_SELECTOR,"#kw")
# ele.send_keys("猫")

# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("赫本")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR,"#kw").clear()
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("明日花")

#获取文本
# text=driver.find_element(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(3) > a").text
# print(text)
# assert text == "1普京访华将停留4天 俄方：非常罕见"

#获取属性值
# text=driver.find_element(By.CSS_SELECTOR,"#su").get_attribute("value")
# print(text)

#获取当前标签和url
# title=driver.title
# url=driver.current_url
# print(title)
# print(url)

#点击百度的图片功能
# print("before"+driver.title)
# print("before"+driver.current_url)
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click()
# print("after"+driver.title)
# print("after"+driver.current_url)


# print("before"+driver.title)
# print("before"+driver.current_url)
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click()
# # 获取当前页的句柄---第一个标签
# curHandle = driver.current_window_handle
# #获取所有句柄
# allHandle = driver.window_handles
# #遍历所有句柄
# for handle in allHandle:
#     if handle != curHandle:
#         #切换句柄
#         # driver.switch_to_window(handle)
#         driver.switch_to.window(handle)
#
# print("after "+driver.title)
# print("after "+driver.current_url)

# driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click()
# print("before"+driver.title)
# print("before"+driver.current_url)
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click()
# time.sleep(2)
# print("after "+driver.title)
# print("after "+driver.current_url)

#设置窗口大小
# #最大化
# driver.maximize_window()
# #最小化
# driver.minimize_window()
# #手动设置
# driver.set_window_size(1024,800)

#添加屏幕截图
# filname="./images/test.png"
# driver.save_screenshot(filname)

#进阶
# filename="autotest-"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
# driver.save_screenshot("./images/"+filename)


#打开新的标签页
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click();
# time.sleep(2)
# #关闭指向的标签页
# driver.close()

#警告弹窗
# driver.get("file:///D:/file/%E6%AF%94%E7%89%B9%E6%95%99%E5%8A%A1/%E6%B5%8B%E8%AF%95/%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6/selenium-html/alert.html#")
# time.sleep(2)
# #点击调起弹窗
# driver.find_element(By.CSS_SELECTOR,"#tooltip").click()
#
# #切换到弹窗
# alert = driver.switch_to.alert
# time.sleep(2)
# #确定/取消
# # alert.accept()
# alert.dismiss()
# time.sleep(2)
# #弹窗关闭之后才能执行下面的代码
# driver.find_element(By.CSS_SELECTOR,"#tooltip").click()

# #确认弹窗
# driver.get("file:///D:/file/%E6%AF%94%E7%89%B9%E6%95%99%E5%8A%A1/%E6%B5%8B%E8%AF%95/%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6/selenium-html/confirm.html")
# driver.find_element(By.CSS_SELECTOR,"body > input[type=button]").click()
# time.sleep(2)
# #切换弹窗
# alert = driver.switch_to.alert
# #确定/取消
# # alert.accept()
# alert.dismiss()

# time.sleep(2)

#提示弹窗---输入框
# driver.get("file:///D:/file/%E6%AF%94%E7%89%B9%E6%95%99%E5%8A%A1/%E6%B5%8B%E8%AF%95/%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B6/selenium-html/Prompt.html")
# time.sleep(2)
# #点击调起弹窗
# driver.find_element(By.CSS_SELECTOR,"body > input[type=button]").click()
# time.sleep(2)
# #输入文本+确定+取消
# alert = driver.switch_to.alert
# #肉眼看不到在弹窗上输入的文本信息
# alert.send_keys("比特就业课")
# time.sleep(2)
# # alert.accept()
# alert.dismiss()


#等待
# driver.implicitly_wait(3) #隐式等待 - 作用域全局

# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("奥黛丽·赫本")
# driver.find_element(By.CSS_SELECTOR,"#su").click()
#
# wait = WebDriverWait(driver,3) #显示等待
# wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")))


# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("奥黛丽·赫本")
# driver.find_element(By.CSS_SELECTOR,"#su").click()
#
# driver.back()
# time.sleep(2)
#
# driver.forward()
# time.sleep(2)
#
# driver.refresh()


#上传文件
# driver.get("file:///C:/Users/20627/AppData/Local/Temp/Rar$EXa59624.44650/selenium-html/upload.html")
# driver.find_element(By.CSS_SELECTOR,"body > div > div > input[type=file]").send_keys("D:\\file\\test.txt")



#添加页面加载策略
# options.page_load_strategy = 'normal' #等待所有的资源加载完成
# options.page_load_strategy = 'eager'  #DOM访问就绪
options.page_load_strategy = 'none'     #完全不阻塞，直接继续往下执行脚本

###创建谷歌浏览器驱动对象
driver=webdriver.Chrome(service=Service(ChromeIns),options=options)
driver.get("https://haokan.baidu.com/")
print(driver.title)


time.sleep(2)
driver.quit()